/*
 * Copyright (C) 2013 www.418log.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.yy.cloudpay.util.db.task;

import android.os.Handler;
import android.os.Message;
import android.os.Process;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

import cn.yy.cloudpay.util.Constant;


// TODO: Auto-generated Javadoc

/**
 * 描述： 执行任务线程（按队列执行）.
 * 每个程序只有1个
 * @author zhaoqp
 * @date 2011-11-10
 * @version v1.0
 */
public class TaskQueue extends Thread {

	/** The tag. */
	private static String TAG = "TaskQueue";

	/** The Constant D. */
	private static final boolean D = Constant.DEBUG;

	/** 等待执行的任务. */
	private static List<TaskItem> mTaskItemList = null;

	/**单例对象 */
	private static TaskQueue mTaskQueue = null;

	/** 停止的标记. */
	private boolean mQuit = false;

	/** 执行完成后的消息句柄. */
	private static Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			TaskItem item = (TaskItem)msg.obj;
			if(item.getListener() instanceof TaskListListener){
				((TaskListListener)item.listener).update((List<?>)item.getResult());
			}else if(item.getListener() instanceof TaskObjectListener){
				((TaskObjectListener)item.listener).update(item.getResult());
			}else{
				item.listener.update();
			}
		}
	};

	/**
	 * 单例构造.
	 */
	public static TaskQueue getInstance() {
		if (mTaskQueue == null) {
			mTaskQueue = new TaskQueue();
		}
		return mTaskQueue;
	}

	/**
	 * 构造执行线程队列.
	 *
	 */
	public TaskQueue() {
		mQuit = false;
		mTaskItemList = new ArrayList<TaskItem>();
		//设置优先级
		Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
		//从线程池中获取
		ExecutorService mExecutorService  = TaskPool.getExecutorService();
		mExecutorService.submit(this);
	}

	/**
	 * 开始一个执行任务.
	 *
	 * @param item 执行单位
	 */
	public void execute(TaskItem item) {
		addTaskItem(item);
	}


	/**
	 * 开始一个执行任务并清除原来队列.
	 * @param item 执行单位
	 * @param clean 清空之前的任务
	 */
	public void execute(TaskItem item,boolean clean) {
		if(clean){
			if(mTaskQueue!=null){
				mTaskQueue.quit();
			}
		}
		addTaskItem(item);
	}

	/**
	 * 描述：添加到执行线程队列.
	 *
	 * @param item 执行单位
	 */
	private synchronized void addTaskItem(TaskItem item) {
		if (mTaskQueue == null) {
			mTaskQueue = new TaskQueue();
			mTaskItemList.add(item);
		} else{
			mTaskItemList.add(item);
		}
		//添加了执行项就激活本线程
		this.notify();

	}

	/**
	 * 描述：线程运行.
	 *
	 * @see java.lang.Thread#run()
	 */
	@Override
	public void run() {
		while(!mQuit) {
			try {
				while(mTaskItemList.size() > 0){

					TaskItem item  = mTaskItemList.remove(0);
					//定义了回调
					if (item.listener != null) {
						item.listener.get();
						//交由UI线程处理
						Message msg = handler.obtainMessage();
						msg.obj = item;
						handler.sendMessage(msg);
					}

					//停止后清空
					if(mQuit){
						mTaskItemList.clear();
						return;
					}
				}
				try {
					//没有执行项时等待
					synchronized(this) {
						this.wait();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
					//被中断的是退出就结束，否则继续
					if (mQuit) {
						mTaskItemList.clear();
						return;
					}
					continue;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 描述：终止队列释放线程.
	 */
	public void quit(){
		mQuit  = true;
		interrupted();
		mTaskQueue  = null;
	}

}

